package gc3d.servlet;

import gc3d.database.DBSettings;
import gc3d.database.GridPovDB;

import java.io.IOException;
import java.io.PrintWriter;

import java.sql.ResultSet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public class insertSettings extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/** doGet Method implementation */
	public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
	}

	/** Example of doPost Method implementation */
	public void doPost(HttpServletRequest req, HttpServletResponse res) {
		//String taskID = "err";
		//String error  = "error";
		//res.setContentType("text/plain");
		res.setContentType("text/html");

		PrintWriter out = null;
		try {
			out = res.getWriter();
		} catch (IOException e) {
			e.printStackTrace();
			return;
		}
		//Set keys = req.getParameterMap().keySet();
		
		//for(Object key : keys){
		//	System.out.println(key);
		//}
		String showTasks = req.getParameter("showtasks");
		String showNodes = req.getParameter("shownodes");
		String showResults = req.getParameter("showresults");
		
		
		GridPovDB db;
		
		
		
		try {
			db = new GridPovDB(DBSettings.getDBURL(),DBSettings.getDBuser(),DBSettings.getDBpass());
			//ResultSet rs = db.query("SELECT task_id from tasks;");
		    //SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd H:mm:ss");
		    
			int rs = db.update("UPDATE settings set value = '"+showNodes+"' where name = 'shownodes'");

			int rs2 = db.update("UPDATE settings set value = '"+showTasks+"' where name = 'showtasks'");

			int rs3 = db.update("UPDATE settings set value = '"+showResults+"' where name = 'showresults'");

		} catch (ClassNotFoundException e1) {
			out.println("ClassNotFoundExceptio");
			// TODO Auto-generated catch block
			out.println(e1.getMessage());
		}
		out.print("got to the end");
	}

	/**
	 * overload info
	 */
	public String getServletInfo() {
		return "GC3D Servlet !";
	}
}
